# -*- coding: utf-8 -*-

'''eof
name:申请企业未结清借贷授信总额
code:EC113_UnsettledDebtAmt
tableName:
columnName:
groups:行内二代企业征信数据
dependencies:EC_DSJ_2D
type:常用指标
datasourceType:在线指标
description:
eof'''

def getTable(data, tableName, occurTimes, iteratorTimes):
    '''
    获取表信息
    :param data:表上层的字典对象
    :param tableName:表的名字
    :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
    :param iteratorTimes:下级字段出现的次数，如果是0代表下级字段可以不出现，如果是1代表下级字段必须出现
    :return:该表全部数据  返回[{}]为异常缺失，返回0为正常缺失，其他返回正常数组
    '''
    try:
        table = data.get(tableName)
        if occurTimes == 0:
            if table is None or (table == "" and iteratorTimes == 0):
                return 0
            elif isinstance(table, list) is False or len(table) == 0 or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
        if occurTimes == 1:
            if table == "" and iteratorTimes == 0:
                return 0
            if table is None or isinstance(table, list) is False or len(table) == 0 \
                    or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
    except:
        return [{}]

def isfloat(x):
    '''
    判断x为float类型且大于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x) > 0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def EC113_UnsettledDebtAmt():
    balance_list = []
    data = EC_DSJ_2D.get('data')
    #EDA 借贷账户信息 [1,1]
    EDA = getTable(data, 'EDA', 1, 0)
    if EDA == [{}]:
        balance_list.append('None')
    elif EDA == 0:
        balance_list.append('None')
    else:
        #ED01 借贷账户信息单元 [0..*]
        ED01 = getTable(EDA[0], 'ED01', 0, 1)
        if ED01 == [{}]:
            balance_list.append('None')
        elif ED01 == 0:
            balance_list.append('None')
        else:
            for i in ED01:
                # ED01A 基本信息段 [1..1]
                ED01A = getTable(i, 'ED01A', 1, 1)
                if ED01A == [{}] or ED01A == 0:
                    balance_list.append('None')
                else:
                    #ED01AD01 账户活动状态 
                    ED01AD01 = ED01A[0].get('ED01AD01', '')
                    #ED01AD02 借贷账户类型
                    ED01AD02 = ED01A[0].get('ED01AD02', '')
                    #ED01AJ01 借款金额
                    ED01AJ01 = ED01A[0].get('ED01AJ01', '')
                    if ED01AD01 not in null_type_list and ED01AD02 not in null_type_list and isfloat(ED01AJ01):
                        if ED01AD02 in ['D1', 'D2', 'R4'] and ED01AD01 == '1':
                            balance_list.append(float(ED01AJ01))
                        elif ED01AD02 == 'R1' and ED01AD01 == '1':
                            balance_list.append(float(ED01AJ01))
                    else:
                        balance_list.append('None')

    #EDB 担保账户信息 [1..1]
    EDB = getTable(data, 'EDB', 1, 0)
    if EDB == [{}]:
        balance_list.append('None')
    elif EDB == 0:
        balance_list.append('None')
    else:
        #ED04 担保账户信息明细单元 [0..*]
        ED04 = getTable(EDB[0], 'ED04', 0, 1)
        if ED04 == [{}]:
            balance_list.append('None')
        elif ED04 == 0:
            balance_list.append('None')
        else:
            for i in ED04:
                #ED04A 基本信息段 [1..1]
                ED04A = getTable(i, 'ED04A', 1, 1)
                #ED04B 在保责任信息段 [1..1]
                ED04B = getTable(i, 'ED04B', 1, 1)
                if ED04A == [{}] or ED04A == 0 or ED04B == [{}] or ED04B == 0:
                    balance_list.append('None')
                else:
                    #ED04BD01 账户活动状态 [1..1]
                    ED04BD01 = ED04B[0].get('ED04BD01', '')
                    #ED04AJ01 金额 [1..1]
                    ED04AJ01 = ED04A[0].get('ED04AJ01', '')
                    if ED04BD01 not in null_type_list and isfloat(ED04AJ01):
                        if ED04BD01 == '1':
                            balance_list.append(float(ED04AJ01))
                    else:
                        balance_list.append('None')

    if len(list(set(balance_list))) == 1 and list(set(balance_list))[0] == 'None':
        return '缺失值'
    factor = [i for i in balance_list if i != 'None']
    return sum(factor)


if __name__ == '__main__':
    import json
    with open(r'C:\Users\zdr\Desktop\samples\最新接口文档与样例\EC_DSJ_2D.json', encoding='utf8') as ff:
        EC_DSJ_2D = json.loads(ff.read())
    result = EC113_UnsettledDebtAmt()
    print(result)
result = EC113_UnsettledDebtAmt()



